﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Controls;
using Model;

namespace TanHungLong
{
    public partial class FrmSoQuyTienMatThuQuy : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private DateTime tuNgay = DateTime.Now;
        private SoDuDauKyTHLBOCollection soDuDauKyTHLBOCollection = null;
        private string tkdu = "";
        #endregion variant

        //=====================================
        #region form
        public FrmSoQuyTienMatThuQuy()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmSoQuyTienMatThuQuy_Load);
            this.Shown += new EventHandler(FrmSoQuyTienMatThuQuy_Shown);
            this.KeyDown += new KeyEventHandler(FrmSoQuyTienMatThuQuy_KeyDown);

            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            this.btnPrint.Click += new EventHandler(btnPrint_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.gridView.RowCellStyle += new RowCellStyleEventHandler(gridView_RowCellStyle);

            this.dtThang.Popup += new EventHandler(dtThang_Popup);
            this.chkTKDU.CheckedChanged += new EventHandler(chkTKDU_CheckedChanged);
        }

        private void FrmSoQuyTienMatThuQuy_Shown(object sender, EventArgs e)
        {
            this.dtThang.Focus();
        }

        private void FrmSoQuyTienMatThuQuy_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnPrint.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                        Print();
                    else
                        if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                            FilterData();
        }

        private void FrmSoQuyTienMatThuQuy_Load(object sender, EventArgs e)
        {
            this.lookUpTKDU.Enabled = false;

            DateTime dtNow = DateTime.Now;
            this.dtThang.DateTime = new DateTime(dtNow.Year, dtNow.Month, 1);

            Utility.InitControl(this.lookUpTKDU, "StrSoHieu", "StrSoHieu");
            MyStyle.SetGridStyle(this.gridView, false);

            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        private void dtThang_Popup(object sender, EventArgs e)
        {
            PopupDateEditForm form = (this.dtThang as IPopupControl).PopupWindow as PopupDateEditForm;
            form.Calendar.View = DateEditCalendarViewType.YearInfo;
        }

        private void chkTKDU_CheckedChanged(object sender, EventArgs e)
        {
            this.lookUpTKDU.Enabled = this.chkTKDU.Checked;
        }
        #endregion control

        //=====================================
        #region button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
            FilterData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            int flag = StaticUtil.GetInt(this.gridView.GetRowCellValue(e.RowHandle, "Flag"), 0);

            if (flag != 0)
            {
                e.Appearance.Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
                e.Appearance.ForeColor = Color.Blue;

                if (e.Column.FieldName == "DienGiai")
                    e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterData()
        {
            #region lay dieu kien
            this.tuNgay = this.dtThang.DateTime;

            int nam2 = this.tuNgay.Year;
            int thang2 = this.tuNgay.Month + 1;

            if (thang2 == 13)
            {
                thang2 = 1;
                nam2++;
            }

            if (this.chkTKDU.Checked)
                this.tkdu = StaticUtil.GetString(this.lookUpTKDU.EditValue, null);
            else
                this.tkdu = null;

            string caption = "SỔ QUỸ TIỀN MẶT NỘI BỘ - THÁNG " + this.tuNgay.ToString("MM/yyyy");
            if (this.tkdu != null)
                caption += " - TKĐƯ: " + this.tkdu + "  -  ";

            this.gridView.ViewCaption = caption;
            #endregion lay dieu kien

            string strTuNgay = "CONVERT(datetime,'" + this.tuNgay.ToString("yyyy-MM") + "-01 00:00:00',120)";
            string strDenNgay = "CONVERT(datetime,'" + nam2.ToString("0000") + "-" + thang2.ToString("00") + "-01 00:00:00',120)";
            string where = "NgayLapPhieu>=" + strTuNgay + " AND NgayLapPhieu<" + strDenNgay;
            if (this.tkdu != null)
                where += " AND ((No=N'111' AND Co=N'" + this.tkdu + "') OR (No=N'" + this.tkdu + "' AND Co=N'111'))";
            else
                where += " AND (No=N'111' OR Co=N'111')";

            SoQuyTienMatThuQuyBOCollection soQuyTienMatThuQuyBOCollection = SoQuyTienMatThuQuyBOCollection.GetSoQuyTienMatThuQuyBOCollection(where, "NgayLapPhieu,So");
            this.bds.DataSource = soQuyTienMatThuQuyBOCollection;
            this.gridView.CheckLoaded();
            SetStt();

            this.btnPrint.Visible = (soQuyTienMatThuQuyBOCollection.Count > 0);


            double[] arrDauKy = this.soDuDauKyTHLBOCollection.GetSoDuDauKyTHLBO(this.tuNgay);
            double NoDauKy = arrDauKy[0];
            double CoDauKy = arrDauKy[1];
            this.numNoDauKy.Value = (decimal)NoDauKy;
            this.numCoDauKy.Value = (decimal)CoDauKy;

            double PhatSinhNo = 0;
            double PhatSinhCo = 0;
            if (soQuyTienMatThuQuyBOCollection.Count > 0)
            {
                SoQuyTienMatThuQuyBO soQuyTienMatThuQuyBO = soQuyTienMatThuQuyBOCollection[soQuyTienMatThuQuyBOCollection.Count - 1];
                PhatSinhNo = StaticUtil.GetDouble(soQuyTienMatThuQuyBO.SoTienChi, 0);
                PhatSinhCo = StaticUtil.GetDouble(soQuyTienMatThuQuyBO.SoTienThu, 0);
            }

            this.numNoPhatSinh.Value = (decimal)PhatSinhNo;
            this.numCoPhatSinh.Value = (decimal)PhatSinhCo;

            this.numDuNoCuoiKy.Value = (decimal)(CoDauKy - NoDauKy + PhatSinhCo - PhatSinhNo);

            this.grpTong.Text = "THÁNG " + this.tuNgay.Month + " NĂM " + this.tuNgay.Year;
            this.lblText.Text = "SỔ QUỸ TIỀN MẶT NỘI BỘ NĂM " + this.tuNgay.Year;
        }

        private void RefreshData()
        {
            this.soDuDauKyTHLBOCollection = SoDuDauKyTHLBOCollection.GetSoDuDauKyTHLBOCollection();

            SoHieuTaiKhoanBOCollection soHieuTaiKhoanBOCollection = SoHieuTaiKhoanBOCollection.GetSoHieuTaiKhoanBOCollection("SoHieu1<>111");
            soHieuTaiKhoanBOCollection.Sort();
            Utility.SetData(this.lookUpTKDU, soHieuTaiKhoanBOCollection, soHieuTaiKhoanBOCollection.Count);
            if (this.lookUpTKDU.EditValue == null)
                if (soHieuTaiKhoanBOCollection.Count > 0)
                    this.lookUpTKDU.EditValue = soHieuTaiKhoanBOCollection[0].StrSoHieu;
        }

        private void Print()
        {
            if (this.gridView.RowCount > 0)
            {
                double[] arrTongHop = new double[5];
                arrTongHop[0] = (decimal.ToDouble(this.numNoDauKy.Value));
                arrTongHop[1] = (decimal.ToDouble(this.numCoDauKy.Value));
                arrTongHop[2] = (decimal.ToDouble(this.numNoPhatSinh.Value));
                arrTongHop[3] = (decimal.ToDouble(this.numCoPhatSinh.Value));
                arrTongHop[4] = (decimal.ToDouble(this.numDuNoCuoiKy.Value));

                Report.rpSoQuyTienMatThuQuy report = new Report.rpSoQuyTienMatThuQuy(this.tuNgay, this.tkdu, (SoQuyTienMatThuQuyBOCollection)this.bds.DataSource, arrTongHop);
                report.ShowPreviewDialog();
            }
        }

        private void SetStt()
        {
            int stt = 0;
            int length = this.gridView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.gridView.GetVisibleRowHandle(i);
                if (index >= 0)
                    if (StaticUtil.GetInt(this.gridView.GetRowCellValue(index, "Flag"), 0) == 0)
                        this.gridView.SetRowCellValue(index, "STT0", ++stt);
            }
        }
        #endregion function
    }
}